/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao.erp;

import com.openbravo.data.loader.TableDBSentenceBuilder;
import e.odbo.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.erp.I_Order;
import cn.ekuma.util.JsonUtil;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataParams;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteParams;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import java.util.Date;
import java.util.HashMap;

/**
 *
 * @author Administrator
 */
public class OrderDAO extends ModifiedLogDAO<Order>{

    public OrderDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(
                  "ORDERS",
                  new Field[]{
                       new Field(I_Order.ID,Datas.STRING,Formats.STRING),
                       new Field(I_Order.DEFINETYPEREF,Datas.STRING,Formats.STRING),
                       new Field(I_Order.ORDERTYPE,Datas.INT,Formats.INT),
                       new Field(I_Order.ORDERID,Datas.STRING,Formats.STRING),
                       new Field(I_Order.CURDATE,Datas.TIMESTAMP,Formats.TIMESTAMP),
                       
                       new Field(I_Order.PERSON,Datas.STRING,Formats.STRING),
                       new Field(I_Order.CUSTOMER,Datas.STRING,Formats.STRING),
                       new Field(I_Order.PRICERATE,Datas.DOUBLE,Formats.DOUBLE),
                       new Field(I_Order.TOTALPRICE,Datas.DOUBLE,Formats.DOUBLE),
                       new Field(I_Order.LOCATIONREF,Datas.STRING,Formats.STRING),
                       
                       new Field(I_Order.LOCATIONREF1,Datas.STRING,Formats.STRING),
                       new Field(I_Order.MEMO,Datas.STRING,Formats.STRING),
                       new Field(I_Order.ATTRIBUTESETINSTANCE_ID,Datas.STRING,Formats.STRING),
                       new Field(I_Order.ATTRIBUTES,Datas.STRING,Formats.STRING),
                       new Field(I_Order.REFERENCEPERSON,Datas.STRING,Formats.STRING),
                       
                       new Field(I_Order.STATE,Datas.STRING,Formats.STRING),
                       new Field(I_Order.PRODUCT,Datas.STRING,Formats.STRING),
                       new Field(I_Order.PRODATTRSETINST_ID,Datas.STRING,Formats.STRING),
                       new Field(I_Order.LASTMODIFIED,Datas.TIMESTAMP,Formats.TIMESTAMP),
                       new Field(I_Order.TOTALTAX,Datas.DOUBLE,Formats.DOUBLE),
                       
                       new Field(I_Order.TOTALCONSULTPRICE,Datas.DOUBLE,Formats.DOUBLE),
                       new Field(I_Order.DEBT,Datas.DOUBLE,Formats.DOUBLE),
                       new Field(I_Order.REFDATE,Datas.TIMESTAMP,Formats.TIMESTAMP),
                       new Field(I_Order.TOTALEXTENDPRICE,Datas.DOUBLE,Formats.DOUBLE),
                       new Field(I_Order.CUSTOMER_ADDR,Datas.STRING,Formats.STRING),
                       new Field(I_Order.DELIVERTYPE,Datas.STRING,Formats.STRING),
                       new Field(I_Order.DELIVERTYPEDESC,Datas.STRING,Formats.STRING)
                      },
                  new int[]{0});
    }

    @Override
    public void writeInsertValues(DataWrite dp, Order obj) throws BasicException {
                        dp.setString(1, obj.getId());
                        dp.setString(2, obj.getDefineType().getId());
                        dp.setInt(3, obj.getOrderType());
                        dp.setString(4, obj.getOrderId());
                        dp.setTimestamp(5, obj.getDate());
                        
                        dp.setString(6, obj.getUser().getId());
                        dp.setString(7, obj.getCustomerId());
                        dp.setDouble(8, obj.getM_dPriceRate());
                        dp.setDouble(9, obj.getTotalPrice());
                        dp.setString(10, obj.getLocationRef());
                        
                        dp.setString(11, obj.getLocationRef1());
                        dp.setString(12, obj.getMemo());
                        dp.setString(13, obj.getAttsetinstid());
                        dp.setString(14,JsonUtil.toString(obj.getProperties()));
                        if (obj.getReference_User() != null) {
                            dp.setString(15, obj.getReference_User().getId());
                        } else {
                            dp.setString(15, null);
                        }
                        
                        dp.setString(16, obj.getCurrentState());
                        dp.setString(17,obj.getConsultProductId());
                        dp.setString(18,obj.getConsultProductAttsetinstid());  
                        dp.setTimestamp(19,obj.getLastModified());
                        dp.setDouble(20,obj.getTotalTax());
                        
                        dp.setDouble(21,obj.getTotalConsultPrice());
                        dp.setDouble(22,obj.getDebt());
                        dp.setTimestamp(23,obj.getM_dRefDate());
                        dp.setDouble(24,obj.getTotalExtendPrice());
                        dp.setString(25, obj.getCustomerAddr());
                        dp.setString(26,obj.getDeliverType());
                        dp.setString(27,obj.getDeliverTypeDesc());
    }

    @Override
    public Class getSuportClass() {
        return Order.class;
    }

    public Order readValues(DataRead dr,Order obj) throws BasicException {
        if(obj==null)
        	obj=new Order();  
        obj.setID(dr.getString(1));
        obj.setDefineType(new OrderType(dr.getString(2)));
        obj.setOrderType(dr.getInt(3));
        obj.setOrderId(dr.getString(4));
        obj.setDate(dr.getTimestamp(5));
        
        obj.setUser(new UserInfo(dr.getString(6)));
        obj.setCustomer(new BusinessPartner(dr.getString(7)));
        obj.setM_dPriceRate(dr.getDouble(8));
        obj.setTotalPrice(dr.getDouble(9));
        obj.setLocationRef(dr.getString(10));
        
        
        obj.setLocationRef1(dr.getString(11));
        obj.setMemo(dr.getString(12));
        obj.setAttsetinstid(dr.getString(13));
        obj.setAttributes(JsonUtil.getHashMap(dr.getString(14)));
        String refUserId=dr.getString(15);
        if(refUserId!=null)
        	obj.setReference_User(new UserInfo(refUserId));
        
        obj.setCurrentState(dr.getString(16));
        obj.setConsultProductId(dr.getString(17));
        obj.setConsultProductAttsetinstid(dr.getString(18));
        obj.setLastModified(dr.getTimestamp(19));
        obj.setTotalTax(dr.getDouble(20));
        
        obj.setTotalConsultPrice(dr.getDouble(21));
        obj.setDebt(dr.getDouble(22));
        obj.setM_dRefDate(dr.getTimestamp(23));
        obj.setTotalExtendPrice(dr.getDouble(24));
        obj.setCustomerAddr(dr.getString(25));
        obj.setDeliverType(dr.getString(26));
        obj.setDeliverTypeDesc(dr.getString(27));
        return obj;
    }
    
    public void updateOrderState(final String orderId,final String state) throws BasicException{
    	new PreparedSentence(s, "UPDATE ORDERS SET STATE=?,LASTMODIFIED=? WHERE ID = ?", SerializerWriteParams.INSTANCE).exec(new DataParams() {
            public void writeValues() throws BasicException {
                	setString(1, state);
                	setTimestamp(2, new Date());
                	setString(3,orderId);
               }
            });
    }
    
    public void updateOrderDebt(final String orderId, final double d) throws BasicException{
    	new PreparedSentence(s, "UPDATE ORDERS SET LASTMODIFIED=?,DEBT=( DEBT - ?) WHERE ID = ?", SerializerWriteParams.INSTANCE).exec(new DataParams() {
            public void writeValues() throws BasicException {
                	setTimestamp(1, new Date());
                	setDouble(2,d);
                	setString(3,orderId);
               }
            });
    }
    
    public Order findOrderByOrderId(String orderId) throws BasicException{
    	return (Order) new PreparedSentence(s,((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE ORDERID=?",SerializerWriteString.INSTANCE,this).find(orderId);
    }
}
